<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/13
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class News extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            $user_name = trim(input('user_name'), ' ');
            $where['new_status'] = ['<>',3];
            if(isset($user_name) && $user_name != ''){
                $where['new_title'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($new_status) && $new_status != ''){
                $where['new_status'] = $new_status;
            }
            if(isset($new_fid) && $new_fid != ''){
                $where['new_fid'] = $new_fid;
            }
            if(isset($new_video) && $new_video != ''){
                $where['new_video'] = $new_video;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_news n')
                ->join('up_news_type nt', 'n.new_type = nt.nt_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $field = 'new_id, new_fid, new_type, new_title,new_thumb,new_sign_left,new_sign_right,new_video';
            $field .= ',new_comment, new_zan, new_time, new_update,new_status,nt_name';
            $lists = Db::table('up_news n')
                ->join('up_news_type nt', 'n.new_type = nt.nt_id', 'left')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('new_status asc,new_update desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function newAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'new_fid' => $new_fid,
                'new_title' => $new_title,
                'new_thumb' => $new_thumb,
                'new_sign_left' => $new_sign_left,
                'new_sign_right' => $new_sign_right,
                'new_video' => $new_video,
                'new_status' => $new_status,
                'new_time' => date('Y-m-d H:i:s'),
                'new_update' => date('Y-m-d H:i:s')
            ];
            if($new_fid == 1){
                if(!$new_type1){
                    return json(['code' => 100, 'msg' => '请选择分类']);
                }
                $data['new_type'] = $new_type1;
            }else{
                if(!$new_type2){
                    return json(['code' => 100, 'msg' => '请选择分类']);
                }
                $data['new_type'] = $new_type2;
            }
            if($new_video == 1){
                $data['new_content'] = $new_content;
            }else{
                if(!$video){
                    return json(['code' => 100, 'msg' => '请上传视频']);
                }
                $data['new_content'] = $video;
            }
            $res = Db::table('up_news')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $types1 = Db::name('news_type')
            ->where('nt_fid',1)
            ->field('nt_id,nt_name,nt_fid')
            ->order('nt_sort desc')
            ->select();
        $types2 = Db::name('news_type')
            ->where('nt_fid',2)
            ->field('nt_id,nt_name,nt_fid')
            ->order('nt_sort desc')
            ->select();
        $this->assign([
            'types1' => $types1,
            'types2' => $types2,
        ]);
        return $this->fetch('news/newadd');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function newEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'new_fid' => $new_fid,
                'new_title' => $new_title,
                'new_thumb' => $new_thumb,
                'new_sign_left' => $new_sign_left,
                'new_sign_right' => $new_sign_right,
                'new_video' => $new_video,
                'new_status' => $new_status,
                'new_time' => date('Y-m-d H:i:s'),
                'new_update' => date('Y-m-d H:i:s')
            ];
            if($new_fid == 1){
                if(!$new_type1){
                    return json(['code' => 100, 'msg' => '请选择分类']);
                }
                $data['new_type'] = $new_type1;
            }else{
                if(!$new_type2){
                    return json(['code' => 100, 'msg' => '请选择分类']);
                }
                $data['new_type'] = $new_type2;
            }
            if($new_video == 1){
                $data['new_content'] = $new_content;
            }else{
                if(!$video){
                    return json(['code' => 100, 'msg' => '请上传视频']);
                }
                $data['new_content'] = $video;
            }
            $res = Db::table('up_news')->where('new_id',$new_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $new_id = input('new_id');
        $info = Db::name('news')
            ->where('new_id',$new_id)
            ->find();
        $types1 = Db::name('news_type')
            ->where('nt_fid',1)
            ->field('nt_id,nt_name,nt_fid')
            ->order('nt_sort desc')
            ->select();
        $types2 = Db::name('news_type')
            ->where('nt_fid',2)
            ->field('nt_id,nt_name,nt_fid')
            ->order('nt_sort desc')
            ->select();
        $this->assign([
            'types1' => $types1,
            'types2' => $types2,
            'info' => $info,
        ]);
        return $this->fetch('news/newedit');
    }
    
    /**
     * 删除
     * @return \think\response\Json
     */
    public function newDel(){
        $new_id = input('id');
        $res = Db::table('up_news')->where('new_id',$new_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 启用
     * @return \think\response\Json
     */
    public function newUp(){
        $new_id = input('id');
        $res = Db::table('up_news')->where('new_id',$new_id)->setField('new_status',1);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    /**
     * 冻结
     * @return \think\response\Json
     */
    public function newDown(){
        $new_id = input('id');
        $res = Db::table('up_news')->where('new_id',$new_id)->setField('new_status',2);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }
    //-------------------------------------------------评论回复------------------------------------------------
    /**
     * 评论
     * @return mixed|\think\response\Json
     */
    public function dynComment(){
        if(request()->isAjax()){
            $user_name = input('user_name');
            $where['comment_type'] = 1;
            $where['comment_new_id'] = session('new_id');
            if(isset($user_name) && $user_name != ''){
                $where['comment_content'] = ['like', '%' . $user_name . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_comment')
                ->join('up_u_user', 'user_id = comment_reply_user_id', 'left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_comment')
                ->join('up_u_user', 'user_id = comment_reply_user_id', 'left')
                ->field('comment_id, comment_content, comment_time,  comment_zan, user_username')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('comment_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $new_id = input('new_id');
        session('new_id', $new_id);
        return $this->fetch('news/dyncomment');
    }

    /**
     * 删除评论
     * @return \think\response\Json
     */
    public function commentDel(){
        $comment_id = input('id');
        $dyn_id = Db::table('up_comment')->where('comment_id', $comment_id)->value('comment_new_id');
        Db::startTrans();
        try{
            // 删除评论
            Db::table('up_comment')->delete($comment_id);
            // 帖子评论数减一
            Db::table('up_news')->where('new_id', $dyn_id)->setDec('new_comment');

            Db::commit();
            return json(['code' => 200, 'msg' => '删除成功']);
        }catch(\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /**
     * 回复
     * @return mixed|\think\response\Json
     */
    public function commentReply(){
        if(request()->isAjax()){
            $user_name = input('user_name');
            $where['comtt_comment_id'] = session('comment_id');
            if(isset($user_name) && $user_name != ''){
                $where['comtt_content'] = ['like', '%' . $user_name . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'u.user_id one_id,u2.user_id reply_id,u.user_username one_name,u2.user_username reply_name';
            $field .= ',m.*';
            $count = Db::table('up_comment_tt')->alias('m')
                ->join('up_u_user u','m.comtt_one_user_id=u.user_id','LEFT')
                ->join('up_u_user u2','m.comtt_reply_user_id=u2.user_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_comment_tt')->alias('m')
                ->join('up_u_user u','m.comtt_one_user_id=u.user_id','LEFT')
                ->join('up_u_user u2','m.comtt_reply_user_id=u2.user_id','LEFT')
                ->field($field)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('comtt_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $comment_id = input('comment_id');
        session('comment_id', $comment_id);
        return $this->fetch('news/commentreply');
    }

    /**
     * 删除回复
     * @return \think\response\Json
     */
    public function comttDel(){
        $comtt_id = input('id');
        $res = Db::table('up_comment_tt')->delete($comtt_id);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    //-------------------------------------------------分类管理------------------------------------------------
    /**
     * 资讯分类
     * @return mixed|\think\response\Json
     */
    public function types(){
        if(request()->isAjax()){
            extract(input());
            $user_name = trim(input('user_name'), ' ');
            $where = [];
            if(isset($user_name) && $user_name != ''){
                $where['nt_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($nt_fid) && $nt_fid != ''){
                $where['nt_fid'] = $nt_fid;
            }
            if(isset($nt_type) && $nt_type != ''){
                $where['nt_type'] = $nt_type;
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_news_type')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_news_type')
                ->where($where)
                ->order('nt_sort desc,nt_update desc')
                ->page($NowPage,$limits)
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    $lists[$k]['typename'] = '自动分配';
                    if($v['nt_type'] == 2){
                        $lists[$k]['typename'] = '手动分配';
                        $lists[$k]['match1'] = Db::table('up_match_fj')
                            ->where('mf_fid',$v['nt_fid'])
                            ->where('mf_fj_matchid',$v['nt_one'])
                            ->field('mf_name,mf_home_name_cn,mf_away_name_cn,mf_match_time')
                            ->find();
                        $lists[$k]['match2'] = Db::table('up_match_fj')
                            ->where('mf_fid',$v['nt_fid'])
                            ->where('mf_fj_matchid',$v['nt_two'])
                            ->field('mf_name,mf_home_name_cn,mf_away_name_cn,mf_match_time')
                            ->find();
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function typeAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'nt_fid' => $nt_fid,
                'nt_name' => $nt_name,
                'nt_type' => $nt_type,
                'nt_sort' => $nt_sort,
                'nt_time' => date('Y-m-d H:i:s'),
                'nt_update' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_news_type')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('news/typeadd');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function typeEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'nt_fid' => $nt_fid,
                'nt_name' => $nt_name,
                'nt_type' => $nt_type,
                'nt_sort' => $nt_sort,
                'nt_update' => date('Y-m-d H:i:s')
            ];
            $oldfid = Db::table('up_news_type')->where('nt_id',$nt_id)->value('nt_fid');
            if($nt_type == 1 || $oldfid != $nt_fid){
                $data['nt_one'] = '';
                $data['nt_two'] = '';
            }
            $res = Db::table('up_news_type')->where('nt_id',$nt_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $nt_id = input('nt_id');
        $info = Db::name('news_type')
            ->where('nt_id',$nt_id)
            ->find();
        $this->assign([
            'info' => $info,
        ]);
        return $this->fetch('news/typeedit');
    }
    /**
     * 分配赛事
     * @return mixed
     */
    public function setMatch(){
        if(Request::instance()->isPost()){
            extract(input());
            $mat = explode(',',$nt_two);
            $data = [
                'nt_one' => $mat[0],
                'nt_update' => date('Y-m-d H:i:s')
            ];
            if(isset($mat[1])){
                $data['nt_two'] = $mat[1];
            }
            $res = Db::table('up_news_type')->where('nt_id',$nt_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $nt_id = input('nt_id');
        $info = Db::name('news_type')
            ->where('nt_id',$nt_id)
            ->find();
        $where = [
            'mf_id' => ['>',0] ,
            'mv_id' => ['>',0] ,
            'mf_fid' => $info['nt_fid'] ,
            'mf_name' => $info['nt_name'] ,
            'mf_state' => ['>=',0] ,
//                'mf_match_time' => 'today' ,
        ];
        $field = 'mf_fj_matchid,mf_name,mf_home_name_cn,mf_away_name_cn,mf_match_time';
        $order = 'mf_state desc,mf_match_time asc';
        $datas = Db::table('up_match_fj mf')
            ->join('up_match_live mv', 'mf.mf_fid=mv.mv_fid and mf.mf_name=mv.mv_name and mf.mf_match_time=mv.mv_match_time and (mf.mf_home_name_cn=mv.mv_home_name_cn or mf.mf_home_name_en=mv.mv_home_name_en)', 'left')
            ->where($where)
            ->order($order)
            ->field($field)
            ->select();
        $this->assign([
            'info' => $info,
            'datas' => $datas,
        ]);
        return $this->fetch('news/setmatch');
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function typeDel(){
        $nt_id = input('id');
        $res = Db::table('up_news_type')->where('nt_id',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}